package com.nus.servlets;

import com.nus.beans.Project;
import com.nus.beans.ProjectRemark;
import com.nus.beans.User;
import com.nus.database.ProjectsDBManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dell
 * Date: 11/1/12
 * Time: 11:41 PM
 * To change this template use File | Settings | File Templates.
 */
public class AddProjectRemarksServlet extends AbstractServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Project project = (Project) request.getSession().getAttribute("project");
        String remark = request.getParameter("remark");
        User user = (User) request.getSession().getAttribute("user");

        ProjectRemark projectRemark = new ProjectRemark();
        projectRemark.setEnteredBy(user);
        projectRemark.setRemark(remark);
        projectRemark.setProjectId(project.getId());

        Connection connection = getUserDatabaseConnection(request);
        ProjectsDBManager projectManager = new ProjectsDBManager(connection);

        projectManager.createProjectRemark(projectRemark);

        projectManager.close();

        request.setAttribute("projects", project.getId());
        RequestDispatcher dispatcher = request.getRequestDispatcher("viewProjectRemarks.do");
        dispatcher.forward(request, response);

    }

}
